using System;
using System.Text;
using System.Collections.ObjectModel;

namespace PersistenceObject.Framework.Expressions
{
    public abstract class BaseExpression : Expression
    {
        protected BaseExpression()
        {
        }

        protected abstract string GetCompareType();

        public override Collection<FieldValue> GetDataParams(IDbDriver dbOperator)
        {
            return this.param;
        }

        public override string GetFilter(IDbDriver dbOperator)
        {
            string paramName = string.Format("{0}{1}", this.Name, this.GetParamCount());
            StringBuilder sb = new StringBuilder();
            sb.Append(this.Name);
            sb.Append(this.GetCompareType());
            //process table.param
            if (paramName.LastIndexOf(".") != -1)
            {
                paramName = paramName.Substring(paramName.LastIndexOf(".") + 1);
            }
            sb.Append(dbOperator.GetParameterName(paramName));
            this.param.Add(new FieldValue(paramName, this.Value));
            return sb.ToString();
        }

        #region Only for SqlServer2005 ServiceBroker
        public override string ServiceBrokerGetFilter(IDbDriver dbOperator)
        {
            string paramName = string.Format("{0}{1}", this.Name, this.GetParamCount());
            StringBuilder sb = new StringBuilder();
            sb.Append(this.Name);
            sb.Append(this.GetCompareType());
            sb.Append(ServiceBroker.Utility.Instance.GetParamValue(this.Value));
            return sb.ToString();
        }
        #endregion
    }
}
